package com.wiretap.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wiretap.dao.MessageDao;
import com.wiretap.po.Message;
import com.wiretap.util.Pager;

public class MessageDaoImpl implements MessageDao {
	private SessionFactory sessionFactory;

	@Override
	public void addMessage(Message msg) throws Exception {
		try {
			this.sessionFactory.getCurrentSession().persist(msg);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public int deleteMessage(String[] ids) throws Exception {
		try {
			String hql = "delete from Message a where ";
			for (int i = 0; i < ids.length; i++) {
				hql += "a.id=?";
				if (i != ids.length - 1) {
					hql += " or ";
				}
			}
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			for (int i = 0; i < ids.length; i++) {
				query.setString(i, ids[i]);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteMessage发生异常");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<Message> queryMessageByOwner(String phone, int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from Message a where a.phone =:phone").setString("phone", phone);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<Message> list = sesion.createQuery("from Message a where a.phone =:phone order by a.sendTime desc").setString("phone", phone).setFirstResult((currentPage - 1) * pageSize)
					.setMaxResults(pageSize).list();
			Pager<Message> pager = new Pager<Message>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("queryMessageByOwner发生异常");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Message getMessageById(String id) throws Exception {
		try {
			return (Message) sessionFactory.getCurrentSession().get(Message.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getMessageById发生异常");
		}
	}

	@Override
	public void updateMessageDeleteStatus(String mid, boolean delete) throws Exception {
		try {
			Message msg = (Message) this.sessionFactory.getCurrentSession().get(Message.class, mid);
			if (msg != null) {
				msg.setDeleted(delete);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public long getMessageCountByPhone(String phone) throws Exception {
		try {
			Query query = sessionFactory.getCurrentSession().createQuery("select count(*) from Message as m where m.phone=:phone").setString("phone", phone);
			return ((Number) query.uniqueResult()).longValue();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public int deleteMessageByPhone(String phone) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from Message m where m.phone=:phone").setString("phone", phone).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteMessageByPhone发生异常");
		}
	}
}
